package com.movie.recomend.controller;

import com.movie.recomend.model.domain.Movie;
import com.movie.recomend.model.domain.User;
import com.movie.recomend.model.recom.Recommendation;
import com.movie.recomend.model.request.MovieRecommendationRequest;
import com.movie.recomend.model.request.TopGenresRecommendationRequest;
import com.movie.recomend.model.request.UserRecommendationRequest;
import com.movie.recomend.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * @Author: Devhui
 * @Date: 2020/8/23 20:23
 * @Email: devhui@ihui.ink
 * @Version: 1.0
 */

@CrossOrigin
@RequestMapping("/rest/movie_CN")
@RestController
public class CNMovieRestApi {

    @Autowired
    private RecommendService recommendService;

    @Autowired
    private MovieService movieService;

    @Autowired
    private UserService userService;

    @GetMapping("/wish")
    public List CN(@RequestParam("username") String username, @RequestParam("num") int num) {
        User user = userService.findByUsername(username);
        // 写死
        Random random = new Random();
        UserRecommendationRequest request = new UserRecommendationRequest(random.nextInt(620), num);
        List<Recommendation> recommendations = recommendService.cn_getCollaborativeFilteringRecommendations(request);
        if (recommendations.size() == 0) {
            String randomGenres = user.getPrefGenres().get(new Random().nextInt(user.getPrefGenres().size()));
            recommendations = recommendService.getTopGenresRecommendations(new TopGenresRecommendationRequest(randomGenres.split(" ")[0], num));
            return movieService.getRecommendeMovies(recommendations);
        }
        List<Movie> moviesCN = movieService.getRecommendeMoviesCN(recommendations);
        return moviesCN;
    }

    @GetMapping("/same")
    public List getSameMovieCN(@RequestParam("mid") int mid, @RequestParam("num") int num) {
        MovieRecommendationRequest request = new MovieRecommendationRequest(mid, num);
        List<Recommendation> recommendations = recommendService.cn_getCollaborativeFilteringRecommendations(request);
        return movieService.getRecommendeMoviesCN(recommendations);
    }

    @GetMapping("/info")
    public Movie getMovieInfo(@RequestParam("mid") int mid) {
        return movieService.findByMidCN(mid);
    }
}
